package com.conner.file_types;

import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Folder {
	
	private File[] fileList;
	private Files[] files;
	
	Scanner file;
	public String directory = "";
	
	public Folder(String directory){
		this.directory = directory;
		populate();
	}
	public Folder(){
	}
	public void addFile(Files f){
		if(files != null){
			Files[] temp = files;
			files = new Files[temp.length+1];
			for(int i = 0; i < temp.length; i++){
				files[i] = temp[i];
			}
			files[files.length-1] = f;
		}else{
			files = new Files[1];
			files[0] = f;
		}
	}
	private void populate(){
		FileFilter fileFilter = new FileFilter() {
		    public boolean accept(File file) {
		        return (!file.isDirectory() && 
		        		!file.getName().endsWith(".exe") &&
		        		!file.getName().endsWith(".rar") &&
		        		!file.getName().endsWith(".zip") &&
		        		!file.getName().endsWith(".gif") &&
		        		!file.getName().endsWith(".mng") &&
		        		!file.getName().endsWith(".jpeg") &&
		        		!file.getName().endsWith(".jpg") &&
		        		!file.getName().endsWith(".png"));
		    }
		};
		fileList = new File(directory).listFiles(fileFilter);
		files = new Files[fileList.length];
		for(int i = 0; i < fileList.length; i++){
			int pos = fileList[i].getName().lastIndexOf('.');//Used to help determine file type
			files[i] = new Files(fileList[i].getName(),readDetails(fileList[i].getAbsoluteFile()),fileList[i].getName().substring(pos+1));
		}
	}
	
	public String readDetails(File f){
		try{ file = new Scanner(f); } 
		catch(FileNotFoundException e){ e.printStackTrace(); }
		
		String details = "";
		while(file.hasNextLine()){
			details += file.nextLine()+"\n";
		}
		return details;
	}
	public File getFile(int index){
		return fileList[index];
	}
	public Files getFile2(int index){
		return files[index];
	}
	public String getFileDetails(int index){
		return files[index].getInformation();
	}
	public String getFileName(int index){
		return files[index].getFileName();
	}
	public String getFileType(int index){
		return files[index].getFileType();
	}
	public int totalFiles(){
		return fileList.length;
	}
	public File[] getFiles(){
		return fileList;
	}
	public Files[] getFolder(){
		return files;
	}
}
